import { Reducer, Effect } from 'umi'
import {
  getReviewerInfo,
  getScoringType,
  createResource,
  downloadReviewerInfo,
  getBatchGradeInfo,
  getDistributeInfo,
  createReviewerInfo
} from '@/api'
import { ResultType } from '@/types/index'
export interface ReviewInfoType {
  name: string
  order: number
  questionCode: string
  type: string
  finalCount: string
  count?: string
  account?: any[]
  children?: any[]
}
interface ScoreType {
  dictKey: string
  dictName: string
  dictType: string
  dictValue: string
}
interface GradeType {
  code: string
  name: string
}

export interface DistributeInfoType {
  name: string
  order: string
  questionCode: string
  type: string
  children: object[]
  child: any[]
}
export interface AccountModelState {
  reviewerInfo: ReviewInfoType[]
  scoreType: ScoreType[]
  gradeInfo: GradeType[]
  distributeInfo: DistributeInfoType[]
}
interface AccountModelType {
  namespace: string
  state: AccountModelState
  reducers: {
    setState: Reducer<AccountModelState>
  }
  effects: {
    function_getReviewerInfo: Effect
    function_getScoringType: Effect
    function_createResource: Effect
    function_updateReviewerInfo: Effect
    function_downloadReviewerInfo: Effect
    function_getBatchGradeInfo: Effect
    function_getDistributeInfo: Effect
    function_createReviewerInfo: Effect
  }
}
export default <AccountModelType>{
  namespace: 'accountModel',
  state: {
    reviewerInfo: [],
    scoreType: [],
    gradeInfo: [],
    distributeInfo: []
  },
  reducers: {
    setState(state, { payload }) {
      return { ...state, ...payload }
    }
  },
  effects: {
    *function_getReviewerInfo(
      { payload: { batchCode, grade } },
      { call, put }
    ) {
      const result: ResultType = yield call(getReviewerInfo, batchCode, grade)
      if (result.code == 200) {
        yield put({
          type: 'setState',
          payload: {
            reviewerInfo: result.obj
          }
        })
      }
    },
    *function_getScoringType({}, { call, put }) {
      const result: ResultType = yield call(getScoringType)
      if (result.code == 200) {
        yield put({
          type: 'setState',
          payload: {
            scoreType: result.obj
          }
        })
      }
    },
    *function_createResource(
      {
        payload: { batchCode, questionCode, grade, type, reviewerCodes },
        callback
      },
      { call }
    ) {
      const result: ResultType = yield call(
        createResource,
        batchCode,
        grade,
        questionCode,
        type,
        reviewerCodes
      )
      if (result.code === 200) {
        callback(result)
      }
    },
    *function_updateReviewerInfo({ payload: { reviewerInfo } }, { _, put }) {
      yield put({
        type: 'setState',
        payload: {
          reviewerInfo
        }
      })
    },
    *function_downloadReviewerInfo(
      { payload: { batchCode }, callback },
      { call }
    ) {
      const result: ResultType = yield call(downloadReviewerInfo, batchCode)
      callback(result)
    },
    *function_getBatchGradeInfo({ payload: { batchCode } }, { call, put }) {
      const result: ResultType = yield call(getBatchGradeInfo, batchCode)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            gradeInfo: result.obj
          }
        })
      }
    },
    *function_getDistributeInfo(
      { payload: { batchCode, grade } },
      { call, put }
    ) {
      const result: ResultType = yield call(getDistributeInfo, batchCode, grade)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            distributeInfo: result.obj
          }
        })
      }
    },
    *function_createReviewerInfo(
      { payload: { batchCode, json }, callback },
      { put, call }
    ) {
      const result: ResultType = yield call(createReviewerInfo, batchCode, json)
      if (result.code === 200) {
        callback(result)
      }
    }
  }
}
